import quoteListCard from '../../components/acessory/quoteListCard'
import defaultImg from '../../components/defaultImg/defaultImg'
export default {
  components: {
    quoteListCard,
    defaultImg,
  },
  data() {
    return {
      titleList: [
        {
          name: '已报价',
        },
        {
          name: '沟通',
        },
        {
          name: '我的订单',
        },
      ],
      list: [],
      current: 0, // tabs组件的current值，表示当前活动的tab选项
      tabIndex: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
      isEnd: false, //是否加载完毕列表
      pageIndex: 1,
      total: NaN,
      showimg: false, //是否显示缺省
      IMlist: [], //沟通列表
      options: [
        {
          text: '删除',
          icon: 'iconshanchu',
          style: {
            backgroundColor: '#dd524d',
          },
        },
      ],
      openIndex: -1, //显示删除按钮的item index -1为全部不显示
      btnWidth: 162,
      // hushow: true,
      showDefault: false,
      friendsList: [], //已聊天好友
      pageLoading: false, //初始化加载效果
    }
  },
  onShow() {
    this.getList()
  },
  mounted() {
    this.$api.news
      .getChatFlowList({
        pageIndex: 1,
        pageSize: 100,
        channel: 0,
        module: 10,
        active_Store_Id: this.$store.state.userInfoDetail.store.storeId,
      })
      .then((res) => {
        if (res.data) {
          this.friendsList = res.data
        }
      })
      .catch((e) => {
        console.log(e)
      })
  },
  methods: {
    //获取列表
    getList() {
      if (this.isEnd) {
        return
      }
      if (this.pageLoading) {
        uni.showLoading()
      }

      this.$api.accessory
        .getQupoteList({
          pageIndex: this.pageIndex,
          pageSize: 10,
        })
        .then((res) => {
          this.total = res.data.total
          this.total = res.data.total
          if (res.data.total === 0) {
            this.showimg = true
          } else if (this.pageIndex === 1) {
            this.showimg = false
            this.list = res.data.list
          } else {
            this.list = this.list.concat(res.data.list)
          }
          if (this.pageIndex * 10 >= this.total) {
            this.isEnd = true
          }
          this.pageIndex++
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
        .catch((e) => {
          console.log(e)
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
    },
    //跳转详情
    goDetail() {
      uni.navigateTo({
        url:
          '/pages_findAccessory/myQuoteDetail/myQuoteDetail?id=' +
          '9697227fd7bc4b8ead63c329a2b2c952',
      })
    },

    // tabs通知swiper切换
    tabsChange(index) {
      if (index == 2) {
        uni.navigateTo({
          url:
            '/pages_order/accessoryOrder/accessoryOrderList/accessoryOrderList?tab=1',
        })
        this.tabIndex = 0
        return
      }
      this.tabIndex = index
      if (index === 1 && this.IMlist.length === 0) {
        if (this.friendsList.length) {
          this.getConversationList()
        } else {
          this.showDefault = true
        }
      }
    },

    open(index) {
      console.log(index)
      this.openIndex = index
      // this.IMlist[index].show = true;
      // this.IMlist.map((val, idx) => {
      // 	if (index != idx) this.IMlist[idx].show = false;
      // });
    },

    close() {
      this.openIndex = -1
    },
    //删除记录
    deleteFn(index) {
      console.log('del')
      return
    },

    contentClick(i) {
      console.log(i)
      this.$store.commit('news/createConversationActive', this.IMlist[i])
      uni.navigateTo({
        url: '/pages_news/room/room?user=' + this.IMlist[i].user,
      })
    },

    // swiper-item左右移动，通知tabs的滑块跟随移动
    transition(e) {
      console.log('transition', e)
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    // 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
    // swiper滑动结束，分别设置tabs和swiper的状态
    animationfinish(e) {
      let current = e.detail.current
      this.$refs.uTabs.setFinishCurrent(current)
      this.swiperCurrent = current
      this.current = current
    },

    // 拉取会话列表
    getConversationList() {
      let promise = this.tim.getConversationList()
      promise
        .then((res) => {
          let conversationList = res.data.conversationList // 会话列表，用该列表覆盖原有的会话列表
          if (conversationList.length) {
            //将返回的会话列表拼接上 用户的基本资料
            //说明：如果已经将用户信息 提交到tim服务端了 就不需要再次拼接
            this.$store.commit('news/updateConversationList', conversationList)
            this.IMlist = []
            for (let i = 0; i < conversationList.length; i++) {
              for (let j = 0; j < this.friendsList.length; j++) {
                if (
                  'C2C' + this.friendsList[j].passive_Store_Id ===
                  conversationList[i].conversationID
                ) {
                  this.IMlist.push({
                    user: conversationList[i].userProfile.nick,
                    img: conversationList[i].userProfile.avatar,
                    userId: conversationList[i].userProfile.userID,
                    lastTime: conversationList[i].lastMessage.lastTime,
                    unreadCount: conversationList[i].unreadCount,
                    conversationID: conversationList[i].conversationID,
                    messageForShow:
                      conversationList[i].lastMessage.messageForShow,
                  })
                  break
                }
              }
            }
            if (this.IMlist.length === 0) {
              this.showDefault = true
            }
          } else {
            this.showDefault = true
          }
        })
        .catch((err) => {
          console.warn('getConversationList error:', err) // 获取会话列表失败的相关信息
        })
    },

    //聊天的节点加上外层的div
    nodesFliter(str) {
      let nodeStr =
        '<div style="align-items: center;word-wrap:break-word;">' +
        str +
        '</div>'
      return nodeStr
    },
  },
}
